package com.xcms.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCHelper {
	private static String className;
	private static String url;
	private static String user;
	private static String password;
	private static ResultSet rs;
	private static PreparedStatement stmt;
	private static Connection conn;
	private static DataSource ds;
	static{
		try {
			InputStream in = JDBCHelper.class.getClassLoader().getResourceAsStream("db.properties");
			Properties props = new Properties();
			props.load(in);
			ds= DruidDataSourceFactory.createDataSource(props);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public  static  DataSource getDataSource(){
		return  ds;
	}
	public static Connection getConnection(){
		try {
			if(conn==null) {
				conn=ds.getConnection();
			}
		} catch (SQLException e) {
			System.out.println("连接服务器错误"+e.getMessage());
		}
		return conn;
	}

	//执行修改
	public  static int update(String sql,Object... params){
		try {
			getConnection();
			stmt=conn.prepareStatement(sql);
			int i=1;
			for (Object param:params) {
				stmt.setObject(i,param);
				i++;
			}
			return  stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("执行更新出错"+e.getMessage());
		}
		return -1;
	}
	//重写update
	public  static int update(String sql,int id,String title){
		try {
			getConnection();
			stmt=conn.prepareStatement(sql);
			stmt.setObject(1,title);
			stmt.setObject(2,id);
			return  stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("执行更新出错"+e.getMessage());
		}
		return -1;
	}
//执行删除
	public static void delete(String sql,Object... params){
		try {
			getConnection();
			stmt=conn.prepareStatement(sql);
			int i=1;
			for (Object param:params) {
				stmt.setObject(i,param);
				i++;
			}
			stmt.execute();
		} catch (SQLException e) {
			System.out.println("执行删除出错"+e.getMessage());
		}
	}
	//执行查询
	public static ResultSet query(String sql,Object... params){
		try {
			getConnection();
			stmt=conn.prepareStatement(sql);
			int i=1;
			for (Object param:params) {
				stmt.setObject(i,param);
				i++;
			}
			rs=stmt.executeQuery();
		} catch (SQLException e) {
			System.out.println("执行查询出错"+e.getMessage());
		}
		return rs;
	}
	//重写查询方法
	public static boolean query1(String sql){
		try {
			getConnection();
			stmt=conn.prepareStatement(sql);
			rs=stmt.executeQuery();
		} catch (SQLException e) {
			System.out.println("执行查询出错"+e.getMessage());
		}

		try {
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	//根据id查询新闻标题
	public static ResultSet get( int id){
		try {
			getConnection();
			String sql="select * from news where id =?";
			stmt=conn.prepareStatement(sql);
			stmt.setObject(1,id);
			rs=stmt.executeQuery();
		} catch (SQLException e) {
			System.out.println("执行查询出错"+e.getMessage());
		}
		return rs;
	}

	public static void close(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs = null;
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt = null;
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn = null;
		}
	}
}
